<?php

/**
 * Created by Sven.
 * Date: 2016/12/29
 * Time: 16:52
 */
class ContentModel extends Model {
    protected  $_table = 'cms_content'; // 该模型对应的表名

    private $id;
    private $title;
    private $nav;
    private $attr;
    private $tag;
    private $keyword;
    private $thumbnail;
    private $source;
    private $author;
    private $info;
    private $content;
    private $commend;
    private $count;
    private $gold;
    private $color;
    private $sort;
    private $readlimit;

    private $in;

    // 拦截器
    public function __get( $name ) {
        return $this->$name;
    }

    public function __set( $name, $value ) {
        // 开启输入验证
        $this->$name = Tool::mysqlString( $value );
    }


    public function getContentList() {
        $_sql = "SELECT 
                                    c.id,
                                    title,
                                    nav_name,
                                    attr,
                                    tag,
                                    keyword,
                                    thumbnail,
                                    source,
                                    author,
                                    info,
                                    commend,
                                    count,
                                    gold,
                                    color,
                                    date
                            FROM 
                              cms_content c
                              LEFT JOIN
                                cms_nav n
                               ON
                               c.nav = n.id 
                            WHERE
                              nav = '$this->nav'
                             ";

        return parent::query( $_sql );
    }

    public function getAllSubContent1( $_id, &$_result, $_nav_model ) {
        // 取出content中nav为$_id 的内容，放入数组
        $this->nav = $_id;
        $_data = $this->getContentList();
        foreach ( $_data as $_row ) {
            $_result[] = $_row;
        }

        $_nav_model->pid = $_id;
        $_navs = $_nav_model->getNavByPid();
        foreach ( $_navs as $_row ) {
            $this->getAllSubContent1( $_row->id, $_result, $_nav_model );
        }

    }

    public function getAllSubContent2() {
        $_sql = "SELECT 
                                    c.id,
                                    title,
                                    n.id nav_id,
                                    nav_name,
                                    attr,
                                    tag,
                                    keyword,
                                    thumbnail,
                                    source,
                                    author,
                                    info,
                                    commend,
                                    count,
                                    gold,
                                    color,
                                    date
                           FROM 
                              cms_content c
                              LEFT JOIN
                                cms_nav n
                               ON
                               c.nav = n.id 
                            WHERE
                              nav in ( $this->in )
                              ORDER BY date DESC
                              {$this->limit}
                             ";

        return parent::query( $_sql );
    }

    public function getListCount(){
        $_nav_model = new NavModel();
        $_nav_model->id = $this->nav;
        $_id_array = array();
        $_nav_model->getAllSubId( $_id_array );

        $this->in = implode(',', $_id_array);

        $_sql = "SELECT 
                                   count(1) as count
                           FROM 
                              cms_content 
                            WHERE
                              nav in ( $this->in )
                             ";

        return parent::one($_sql)->count;
    }


    public function addContent() {
        $_sql = "INSERT 
                                  INTO 
                                  cms_content(
                                    title,
                                    nav,
                                    attr,
                                    tag,
                                    keyword,
                                    thumbnail,
                                    source,
                                    author,
                                    info,
                                    content,
                                    commend,
                                    count,
                                    gold,
                                    color,
                                    sort,
                                    readlimit,
                                    date
                                  ) 
                                  VALUES (
                                      '$this->title',
                                      '$this->nav',
                                      '$this->attr',
                                      '$this->tag',
                                      '$this->keyword',
                                      '$this->thumbnail',
                                      '$this->source',
                                      '$this->author',
                                      '$this->info',
                                      '$this->content',
                                      '$this->commend',
                                      '$this->count',
                                      '$this->gold',
                                      '$this->color',
                                      '$this->sort',
                                      '$this->readlimit'
                                      NOW()
                                  );";

        return parent::execute( $_sql );
    }

    public function updateContent() {
        $_sql = "UPDATE 
                                  cms_content
                                  SET
                                    title = '$this->title',
                                    nav = '$this->nav',
                                    attr =  '$this->attr',
                                    tag = '$this->tag',
                                    keyword = '$this->keyword',
                                    thumbnail = '$this->thumbnail',
                                    source = '$this->source',
                                    author = '$this->author',
                                    info = '$this->info',
                                    content = '$this->content',
                                    commend = '$this->commend',
                                    count = '$this->count',
                                    gold = '$this->gold',
                                    color = '$this->color',
                                    sort = '$this->sort',
                                    readlimit = '$this->readlimit',
                                    date = NOW()
                                    
                                WHERE id =  '$this->id'; 
                                    
                                  ";

        return parent::execute( $_sql );
    }

    public function setCount(){
        $_sql = "UPDATE 
                                  cms_content
                                  SET
                                    count = count+1
                                    
                                WHERE id =  '$this->id'; 
                                  ";
        return parent::execute($_sql);
    }


}